<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="../../res/layui/layui.js"></script>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" type="text/css">
    <script src="../../res/js/Tools.js" charset="utf-8"></script>
    <script src="../../res/js/jquery-3.1.1.min.js" charset="utf-8"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
</head>
<body>
<input type="hidden" id="title">
<input type="hidden" id="date1">
<input type="hidden" id="date2">
<input type="hidden" id="issuer">
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
    <div id="kool">
        <div class="layui-inline">
            <input type="text" style="width: 150px" placeholder="标题" class="layui-input" id="attachName">
        </div>
        <div class="layui-inline">
            <input class="layui-input" style="width: 120px" name="newsTimeS" id="newsTimeS"
                   placeholder="开始时间" autocomplete="off">
        </div>
        <div class="layui-inline">
            <input class="layui-input" style="width: 120px" name="newsTimeE" id="newsTimeE"
                   placeholder="结束时间" autocomplete="off">
        </div>
        <div class="layui-inline" id="fbUser">
            <input type="text" placeholder="发布人" style="width: 130px" class="layui-input" id="attachPublisher">
        </div>
        <input type="button" value="查询" class="layui-btn" lay-event="select">
        <input type="button" value="刷新" class="layui-btn layui-btn-normal" lay-event="reload">

        <button class="layui-btn layui-btn-warm" lay-event="add">新增数据</button>
        <button class="layui-btn layui-btn-warm" lay-event="delAll">批量删除</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    {{# if(d.remark==d.attachPublisher){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit" >编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" >删除</a>
    {{# } else{}}
    <a class="layui-btn layui-btn-xs layui-btn-disabled">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" >删除</a>
    {{# } }}
</script>
<script type="text/html" id="switchTp33">
    <a href="../..{{d.file1}}" target="_blank" class="layui-btn layui-btn-xs layui-btn-normal">下载</a>
    </script>
<script type="text/html" id="switchTp332">
    <a href="../..{{d.file2}}" target="_blank" class="layui-btn layui-btn-xs layui-btn-normal">下载</a>
</script>
<script type="text/html" id="switchTp333">
    <a href="../..{{d.file3}}" target="_blank" class="layui-btn layui-btn-xs layui-btn-normal">下载</a>
</script>
<script type="text/html" id="switchTpl2">
    {{# if(d.auditState==10){ }}
    发布
    {{# } else if(d.auditState==20){}}
    等待审核
    {{# } else if(d.auditState==25){}}
    未通过
    {{# } else if(d.auditState==30){}}
    通过
    {{# } }}
    </script>
<script type="text/html" id="switchTpl3">
    {{# if(d.attachType==10){ }}
    通知公告
    {{# } else if(d.attachType==20){}}
    公务信息
    {{# } else if(d.attachType==40){}}
    学习资料
    {{# } else if(d.attachType==30){}}
    文件政策
    {{# } }}
</script>
<script type="text/html" id="switchTpl4">
    {{# if(d.attachValidTime!=null){ }}
    <div>{{layui.util.toDateString(d.attachValidTime, 'yyyy-MM-dd')}}</div>
    {{# } }}
</script>

<script type="text/html" id="switchTpl">
    <input type="checkbox" name="sex" value="{{d.attachId}}" disabled="" lay-skin="switch" lay-text="是|否" lay-filter="sexDemo" {{ d.isAudit == 2 ? 'checked' : '' }}>
</script>
<script type="text/html" id="switchTpl1">
    <input type="checkbox" name="sex" value="{{d.isva}}" disabled="" lay-skin="switch" lay-text="是|否" lay-filter="sexDemo" {{ d.isva == 1 ? 'checked' : '' }}>
</script>
<script>
    $(function(){
        titleupdate();//标题更改
        newTime();//加入时间框
        formvalue();//条件框赋值
        $("#reload").click(function(){
            var url="";
            var attachType=getQueryString("attachType");
            url="../../AtsAttachmentAction/select";
            if(attachType!=null) {
                url += "?auditState=10&isva=1&attachType=" + attachType;
                var urls = "../../AtsAttachmentAction/selectEmp";
                var look = "";
                $.post(urls, null, function (obj) {
                    $.each(obj, function (index, item) {
                        if (item.attachId != null) {
                            look += item.attachId;
                        }
                    })
                }, 'json')
                url += "&attachIdLike=" + look;
            }
            table2.reload({
                url:url,
                where: {
                    attachName: null,
                    beginDate:null,
                    attachPublisher:null,
                    endDate:null
                },
                page: { curr: 1 }
            });
        })
    })
    function titleupdate(){
        var attachType=getQueryString("attachType");
        if(attachType==10){
            document.title = '通知公告';
        }else if(attachType==20){
            document.title = '公务信息';
        }else if(attachType==30){
            document.title = '文件政策';
        }else if(attachType==40){
            document.title='学习资料';
        }
    }
    //选择时间框的方法
    function newTime(){
        layui.use('laydate', function(){
            var laydate = layui.laydate;

            //常规用法
            laydate.render({
                elem: '#newsTimeS'
                ,type: 'date'
            });
            //常规用法
            laydate.render({
                elem: '#newsTimeE'
                ,type: 'date'    //可以选择时间
            });
        });
    }
    //给隐藏条件框赋值
    function hidevalue(){
        $("#title").val($("#attachName").val());
        $("#date1").val($("#newsTimeS").val());
        $("#date2").val($("#newsTimeE").val());
        $("#issuer").val($("#attachPublisher").val());
    }
    //条件查询框赋值
    function formvalue(){
        $("#attachName").val($("#title").val());
        $("#newsTimeS").val($("#date1").val());
        $("#newsTimeE").val($("#date2").val());
        $("#attachPublisher").val($("#issuer").val());
    }
</script>

<script>

    //全局同步
    $.ajaxSetup({
        async:false
    });

    var table2 = "";
    var attachType = getQueryString("attachType");
    var url="../../AtsAttachmentAction/select";
    if(attachType!=null){
        url+="?auditState=10&attachType="+attachType
            var urls="../../AtsAttachmentAction/selectEmp";
            var look="";
            $.post(urls,null,function(obj){
                $.each(obj,function(index,item){
                    if(item.attachId!=null){
                        look+=item.attachId;
                    }
                })
            },'json')
            url+="&isva=1&attachIdLike="+look;
    }
    layui.use('table', function(){
        var table = layui.table
            ,form = layui.form;
        table2=table.render({
            elem: '#test'
            ,url:url
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
           // ,toolbar:false
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '通知公告表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'attachType', title:'附件类型', width:100, templet: '#switchTpl3'}
                ,{field:'attachName', title:'标题', width:100}
                ,{field:'attachContent', title:'内容', width:100}
                ,{field:'isAudit', title:'是否需要审核', width:100,  templet: '#switchTpl'}
                // ,{field:'isva', title:'是否有效', width:100, templet: '#switchTpl1'}
                ,{field:'file1', title:'附件1', width:100, templet: '#switchTp33'}
                ,{field:'file2', title:'附件2', width:100,  templet: '#switchTp332'}
                ,{field:'file3', title:'附件3', width:100, templet: '#switchTp333'}
                ,{field:'auditState', title:'审核状态', width:100, templet: '#switchTpl2'}
                ,{field:'attachPublisher', title:'发布人', width:100}
                ,{field:'attachTime', title:'发布时间', width:120,templet: "<div>{{layui.util.toDateString(d.attachTime, 'yyyy-MM-dd')}}</div>"}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true
        });
        var userId = getQueryString("userId");
        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'delAll':
                    var data = checkStatus.data;
                    //layer.alert(JSON.stringify(data));
                    layer.confirm('真的删除行么', function(index) {
                        $.each(data, function (index, item) {
                            var look="../../AtsAttachmentEmpAction/findUser";
                            $.post(look,null,function(obj){
                                if(obj.userName==item.attachPublisher||obj.userName=='admin'){
                                    var url="../../AtsAttachmentAction/updateByPrimaryKeySelective";
                                    var data={attachId:item.attachId,isva:0};
                                    $.post(url,data,function(objs){
                                        if(objs.num==1){
                                            layer.msg(objs.msg);
                                        }else{
                                            layer.msg(objs.msg);
                                        }
                                        table2.reload();
                                    })
                                }else{
                                    var url="../../AtsAttachmentEmpAction/userIdupdate";
                                    var data={attachId:item.attachId,isva:0,userId:obj.userId};
                                    $.post(url,data,function(objs){
                                        if(objs.num==1){
                                            layer.msg(objs.msg);
                                        }else{
                                            layer.msg(objs.msg);
                                        }
                                        table2.reload();
                                    })
                                }
                            },'json')
                        })
                    })
                    break;
                case 'audit'://审核
                    // var urlk="../../AtsAttachmentEmpAction/findUser";
                    // $.post(urlk,null,function(obj){
                    //     if(obj.userName=='admin'){
                            location.href="audit.html?attachType="+getQueryString("attachType");
                    //     }else{
                    //         layer.msg("你没有这个权限");
                    //     }
                    // },'json')
                    break;
                case 'add'://新增数据
                    layer.open({
                        type: 2,
                        title: '辅助模块新增界面',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['580px', '90%'],
                        content: 'addUpdate.html?attachType='+getQueryString("attachType"), //iframe的url
                        end:function () {
                            formvalue();//给条件框赋值
                            newTime();//加入时间框
                        }
                    });
                    break;
                case 'select':
                    hidevalue();//给隐藏条件框赋值
                    table2.reload({
                        url:url,
                        where: {
                            attachName: $("#attachName").val(),
                            beginDate:$("#newsTimeS").val(),
                            endDate:$("#newsTimeE").val(),
                            attachPublisher:$("#attachPublisher").val()
                        },
                        page: { curr: 1 }
                    });
                    formvalue();//给条件框赋值
                    newTime();//加入时间框
                    break;
                case 'reload':
                    window.location.reload();
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var datas = obj.data;
            if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        var look="../../AtsAttachmentEmpAction/findUser";
                        $.post(look,null,function(obj){
                            if(obj.userName==datas.attachPublisher||obj.userName=='admin'){
                                var url="../../AtsAttachmentAction/updateByPrimaryKeySelective";
                                var data={attachId:datas.attachId,isva:0};
                                $.post(url,data,function(objs){
                                    if(objs.num==1){
                                        layer.msg(objs.msg);
                                    }else{
                                        layer.msg(objs.msg);
                                    }
                                    table2.reload();
                                })
                            }else{
                                var url="../../AtsAttachmentEmpAction/userIdupdate";
                                var data={attachId:datas.attachId,isva:0,userId:obj.userId};
                                $.post(url,data,function(objs){
                                    if(objs.num==1){
                                        layer.msg(objs.msg);
                                    }else{
                                        layer.msg(objs.msg);
                                    }
                                    table2.reload();
                                })
                            }
                        },'json')
                        table2.reload();
                    });
            }else if(obj.event === 'edit'){
                    layer.open({
                        type: 2,
                        title: '辅助模块编辑界面',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['580px', '90%'],
                        content: 'addUpdate.html?attachId='+datas.attachId+'&attachType='+getQueryString("attachType"), //iframe的url
                        end:function () {
                            formvalue();//给条件框赋值
                            newTime();//加入时间框
                        }
                    });
            }
        });

    });
</script>
</body>
</html>